package com.ymm.lulutong.service.impl;

import com.ymm.lulutong.domain.ShipperProfileDomainService;
import com.ymm.lulutong.dto.OrderShipperDTO;
import com.ymm.lulutong.dto.ShowShipperDTO;
import com.ymm.lulutong.entity.Shipper;
import com.ymm.lulutong.request.QueryShipperDTO;
import com.ymm.lulutong.service.ShipperProfileService;
import com.ymm.lulutong.service.translator.ShipperTranslator;
import com.ymm.lulutong.utils.Result;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author guorui
 */
//@Service(version = "local")
@Service(version = "server")
@Component
public class ShipperProfileServiceImpl implements ShipperProfileService {
    @Resource
    private ShipperProfileDomainService shipperProfileDomainService;

    @Override
    public Result<OrderShipperDTO> getOrderShipperDTO(Long shipperId) {
        Result<Shipper> result = shipperProfileDomainService.getShipperByShipperId(shipperId);
        Shipper shipper = result.getData();
        if (shipper == null) {
            return Result.fail(null, result.getCode(), result.getMessage());
        }
        return Result.success(ShipperTranslator.shipper2OrderShipperDTO(shipper));
    }

    @Override
    public Result<List<ShowShipperDTO>> listShowShipperDTOsByPage(Integer pageNum, Integer pageSize) {

        Result<List<Shipper>> result = shipperProfileDomainService.listShippersByPage(pageNum, pageSize);
        List<Shipper> shippers = result.getData();
        if (shippers == null) {
            return Result.fail(null, result.getCode(), result.getMessage());
        }
        return Result.success(ShipperTranslator.listShipper2ListShowShipperDTO(shippers));
    }

    @Override
    public Result<List<ShowShipperDTO>> listShowShipperDTOsOnConditionByPage(Integer pageNum, Integer pageSize, QueryShipperDTO queryShipperDTO) {
        Shipper shipperCondition = ShipperTranslator.queryShipperDTO2Shipper(queryShipperDTO);
        Result<List<Shipper>> result = shipperProfileDomainService.listShippersOnConditionByPage(pageNum, pageSize, shipperCondition);
        List<Shipper> shippers = result.getData();
        if (shippers == null) {
            return Result.fail(null, result.getCode(), result.getMessage());
        }
        return Result.success(ShipperTranslator.listShipper2ListShowShipperDTO(shippers));
    }

    @Override
    public Result<List<OrderShipperDTO>> listOrderShipperDTOsByShipperIds(List<Long> shipperIds) {
        List<Shipper> shippers = new ArrayList<>();
        for (Long shipperId : shipperIds) {
            Result<Shipper> result = shipperProfileDomainService.getShipperByShipperId(shipperId);
            if (!result.isSuccess()) {
                return Result.fail(null, result.getCode(), result.getMessage());
            }
            Shipper shipper = result.getData();
            shippers.add(shipper);
        }
        return Result.success(ShipperTranslator.listShipper2ListOrderShipperDTO(shippers));
    }
}
